package com.example.rmation.service.impl;

import com.example.rmation.common.MyExecption;
import com.example.rmation.mapper.MessageMapper;
import com.example.rmation.model.entity.Message;
import com.example.rmation.model.entity.User;
import com.example.rmation.model.request.AddMessageRequest;
import com.example.rmation.model.request.AllMessageRequest;
import com.example.rmation.model.request.UpdateMessageRequest;
import com.example.rmation.service.MessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    MessageMapper messageMapper;

    @Override
    public PageInfo messageAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<AllMessageRequest> MessageList = messageMapper.studentMessageAll();
        PageInfo pageInfo = new PageInfo(MessageList);
        return pageInfo;
    }

    @Override
    public void studentMessageUpdate(UpdateMessageRequest updateMessageRequest) {
        Message message = new Message();
        BeanUtils.copyProperties(updateMessageRequest,message);
        //效验通过后进行的业务
        int updateCount = messageMapper.updateByPrimaryKeySelective(message);
        if (updateCount == 0) {
            throw new MyExecption(5000,"更新失败!");
        }
    }

    @Override
    public PageInfo studentMessageLike(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        List<AllMessageRequest> MessageList = messageMapper.studentMessageLike(name);
        PageInfo pageInfo = new PageInfo(MessageList);
        return pageInfo;
    }

    @Override
    public void addMessage(AddMessageRequest addMessageRequest) {
        Message message = new Message();
        BeanUtils.copyProperties(addMessageRequest,message);
        //效验通过后进行的业务
        int addCount = messageMapper.insertSelective(message);
        if (addCount == 0) {
            throw new MyExecption(5000,"上传失败!");
        }
    }

    @Override
    public PageInfo messageOneself(Integer pageNum, Integer pageSize, String username) {
        PageHelper.startPage(pageNum,pageSize);
        List<AllMessageRequest> MessageList = messageMapper.messageOneself(username);
        PageInfo pageInfo = new PageInfo(MessageList);
        return pageInfo;
    }
}
